package com.onstar.cn.weight;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.onstar.cn.common.cache.ImageCache;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.BitmapUtil;

/**
 * 多层叠加点击自定义控件，用于不规则边缘控件实现
 * 
 * @author cwjtojava
 * 
 */
public class MultiLayerTouchView extends FrameLayout {

	/**
	 * 图层默认闪烁次数
	 */
	private final int DEFAULT_BLINK_TIMES = 2;

	/**
	 * 图层默认闪烁周期
	 */
	private final long DEFAULT_BLINK_PERIOD = 200;

	/**
	 * 闪烁计时器
	 */
	private Timer blinkTimer;
	private Map<String, TouchViewLayerImageView> map = new HashMap<String, TouchViewLayerImageView>();
	private List<ImageView> imageViews = new ArrayList<ImageView>();
	private ImageCache imageCache;
	public MultiLayerTouchView(Context context) {
		super(context);
		imageCache = ImageCache.getInstance(context);
	}

	public MultiLayerTouchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		imageCache = ImageCache.getInstance(context);
	}

	public MultiLayerTouchView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		imageCache = ImageCache.getInstance(context);
	}

	/**
	 * 添加图层<br>
	 * 应保证每个图层的大小一致从而避免图像错位<br>
	 * 最先添加的图层在最底部
	 * 
	 * @param layer
	 *            添加的图层，并不强制校验图层大小
	 */
	public void addLayer(TouchViewLayer layer) {
		TouchViewLayerImageView imageView = new TouchViewLayerImageView(getContext(), layer);
		if (layer.getLayerName() != null) {
			map.put(layer.getLayerName(), imageView);
		}
		imageViews.add(imageView);
		addView(imageView);
	}

	@Override
	public void removeAllViewsInLayout() {
		for (ImageView imageView : imageViews) {
			BitmapUtil.recycleImageView(imageView);
		}
		super.removeAllViewsInLayout();
	}

	/**
	 * 删除图层<br>
	 * 
	 * @param layerName
	 *            图层名称
	 */
	public void removeLayer(String layerName) {
		TouchViewLayerImageView imageView = getImageView(layerName);
		if (imageView != null) {
			LogManager.getInstance().i("animation removeLayer");
			map.remove(layerName);
			this.removeViewInLayout(imageView);
			imageView.setVisibility(View.GONE);
		}
	}

	public TouchViewLayerImageView getImageView(String layerName) {
		return map.get(layerName);
	}

	public void addLayer(int drawableNormalRes, int drawableDownRes, OnClickListener listener, boolean isAnimation) {
		addLayer(new TouchViewLayer(imageCache.getCache(drawableNormalRes, true), imageCache.getCache(drawableDownRes, true), listener));
	}

	/**
	 * 添加图层<br>
	 * 应保证每个图层的大小一致从而避免图像错位<br>
	 * 最先添加的图层在最底部
	 * 
	 * @param drawableNormalRes
	 *            普通状态图片资源
	 * @param drawableDownRes
	 *            按下状态图片资源
	 * @param listener
	 *            事件响应
	 */
	public void addLayer(int drawableNormalRes, int drawableDownRes, OnClickListener listener) {
		addLayer(new TouchViewLayer(imageCache.getCache(drawableNormalRes, true), imageCache.getCache(drawableDownRes, true), listener));
	}

	/**
	 * 添加图层<br>
	 * 应保证每个图层的大小一致从而避免图像错位<br>
	 * 最先添加的图层在最底部
	 * 
	 * @param drawableNormalRes
	 *            普通状态图片资源
	 */
	public void addLayer(int drawableNormalRes) {
		addLayer(new TouchViewLayer(imageCache.getCache(drawableNormalRes, true)));
	}

	/**
	 * 添加图层<br>
	 * 应保证每个图层的大小一致从而避免图像错位<br>
	 * 最先添加的图层在最底部
	 * 
	 * @param layerName
	 *            图层名称
	 * @param drawableNormalRes
	 *            普通状态图片资源
	 * @param drawableDownRes
	 *            按下状态图片资源
	 */
	public void addLayer(String layerName, int drawableNormalRes, int drawableDownRes) {
		addLayer(new TouchViewLayer(layerName, imageCache.getCache(drawableNormalRes, true), imageCache.getCache(drawableDownRes, true)));
	}

	/**
	 * 添加图层<br>
	 * 应保证每个图层的大小一致从而避免图像错位<br>
	 * 最先添加的图层在最底部
	 * 
	 * @param layerName
	 *            图层名称
	 * @param drawableNormalRes
	 *            普通状态图片资源
	 */
	public void addLayer(String layerName, int drawableNormalRes) {
		addLayer(new TouchViewLayer(layerName, imageCache.getCache(drawableNormalRes, true), null));
	}

	/**
	 * 闪烁图层
	 * 
	 * @param layerName
	 *            图层名称
	 * @param times
	 *            闪烁次数
	 * @param period
	 *            闪烁周期
	 */
	public void blinkLayer(final String layerName, final int times, final long period) {

		if (layerName == null || layerName.length() == 0) {
			return;
		}

		if (times <= 0 || period < 50) { // 闪烁速度过快忽略
			return;
		}

		cancelBlinkLayer();

		int childCount = getChildCount();

		for (int i = 0; i < childCount; i++) {
			View view = getChildAt(i);

			if (view instanceof TouchViewLayerImageView) {

				final TouchViewLayerImageView touchView = (TouchViewLayerImageView) view;

				if (layerName.equals(touchView.getLayerName())) { // 找到对应名称的层

					blinkTimer = new Timer();
					blinkTimer.schedule(new TimerTask() {

						private int count = times;

						@Override
						public void run() {
							if (count-- <= 0) { // 限定执行次数
								cancelBlinkLayer();
								return;
							}

							touchView.blink();
						}
					}, 0, period);
				}
			}
		}
	}

	/**
	 * 闪烁图层
	 * 
	 * @param layerName
	 */
	public void blinkLayer(String layerName) {
		blinkLayer(layerName, DEFAULT_BLINK_TIMES, DEFAULT_BLINK_PERIOD);
	}

	/**
	 * 取消图层闪烁
	 */
	public void cancelBlinkLayer() {
		if (blinkTimer != null) {
			blinkTimer.cancel();
			blinkTimer = null;
		}
	}
}
